home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11240 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: hpscit.sc.hp.com!news
  2. From: "N. Srinivasan" <ns@india.hp.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Operator overloading
  5. Date: Wed, 13 Mar 1996 18:19:32 +0500
  6. Organization: Hewlett-Packard Company
  7. Message-ID: <3146CB64.545C@india.hp.com>
  8. References: <313F19B5.41C6@lfa.uni-wuppertal.de> <314575D9.3A2A@teambca.com>
  9. NNTP-Posting-Host: victory.india.hp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.05 9000/712)
  14.  
  15. Mark VanTassel wrote:
  16. > > const T& operator[](unsigned long int index) const;
  17. > >
  18. > > I know, it is an subscription overloading, for something like
  19. > > v[i], where i is of type unsigned long int and v of type T.
  20. > >
  21. > > What's the meaning of the const at the end
  22. > > and the beginning of the line ?
  23. > >
  24. > The const at the end says that this member function (it must be a member, since it only defines
  25. > one argument) cannot modify the state of the object to which it is being applied (the
  26. > pseudo-array which is being "indexed").
  27. > The const at the beginning says that the object (of type T) to which a reference is returned
  28. > cannot be modified by the caller of this "index" operation.
  29. > (Note that both can be overridden by casting the constness away. This should NEVER be done,
  30. > except in certain circumstances ;-)
  31. The const at the end makes the function a "const function".
  32. This means that it can be called for a const instance of the class in which it is
  33. defined.
  34. Functions which are not "const" cannot be called for const instances.
  35. -- 
  36. Regards,
  37. ns(N. Srinivasan)
  38. ------------------
  39. "Unix was not designed to stop you from doing stupid things, because 
  40.  that would also stop you from doing clever things" -- Doug Gwyn
  41.